package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.YingbaoRotationImages;
import cn.stylefeng.guns.modular.yingbao.mapper.YingbaoRotationImagesMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.YingbaoRotationImagesParam;
import cn.stylefeng.guns.modular.yingbao.model.result.YingbaoRotationImagesResult;
import  cn.stylefeng.guns.modular.yingbao.service.YingbaoRotationImagesService;
import cn.stylefeng.guns.redis.RedisUtil;
import cn.stylefeng.guns.sys.modular.system.model.UploadResult;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 轮播图 服务实现类
 * </p>
 *
 * @author jj
 * @since 2019-12-05
 */
@Service
public class YingbaoRotationImagesServiceImpl extends ServiceImpl<YingbaoRotationImagesMapper, YingbaoRotationImages> implements YingbaoRotationImagesService {
    @Autowired
    FileInfoService fileInfoService;
    @Override
    public void add(YingbaoRotationImagesParam param){
        YingbaoRotationImages entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(YingbaoRotationImagesParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(YingbaoRotationImagesParam param){
        YingbaoRotationImages oldEntity = getOldEntity(param);
        YingbaoRotationImages newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public YingbaoRotationImagesResult findBySpec(YingbaoRotationImagesParam param){
        return null;
    }

    @Override
    public List<YingbaoRotationImagesResult> findListBySpec(YingbaoRotationImagesParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(YingbaoRotationImagesParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<YingbaoRotationImages> findPageBySpec() {
        QueryWrapper<YingbaoRotationImages> query =new QueryWrapper();
        query.orderByAsc("position");
        List<YingbaoRotationImages> yingbaoRotationImages = baseMapper.selectList(query);
        return yingbaoRotationImages;
    }

    /**
     * 上传文件（自定义路径）
     *
     * @author fengshuonan
     * @Date 2019-05-04 17:18
     */
    @Override
    public UploadResult uploadFile(MultipartFile file) {
        String fileSavePath = ConstantsContext.getFileUploadPath();
        return fileInfoService.uploadFile(file, fileSavePath);
    }

    private Serializable getKey(YingbaoRotationImagesParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private YingbaoRotationImages getOldEntity(YingbaoRotationImagesParam param) {
        return this.getById(getKey(param));
    }

    private YingbaoRotationImages getEntity(YingbaoRotationImagesParam param) {
        YingbaoRotationImages entity = new YingbaoRotationImages();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
